package com.game.sdk.pay.heepay;


import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.game.sdk.YTAppService;
import com.game.sdk.domain.PaymentCallbackInfo;
import com.game.sdk.domain.PaymentErrorMsg;
import com.game.sdk.domain.ResultCode;
import com.game.sdk.init.NetCallBack;
import com.game.sdk.ui.ChargeActivity;
import com.game.sdk.util.ActivityTaskManager;
import com.game.sdk.util.Constants;
import com.game.sdk.util.DialogUtil;
import com.game.sdk.util.MResource;
import com.game.sdk.util.Util;
import com.heepay.plugin.api.HeepayPlugin;

public class HeePayActivity extends Activity {
	private String orderid;
	private double charge_money;// 需要充值的金额
	private String serverid;// 充值的服务器id；
	private String productname;// 充值游戏名称
	private String productdesc;// 产品描述
	private String fcallbackurl;// 充值回调地址，由游戏方传递
	private String roleid;// 角色id；
	private String attach;// 游戏方传递的拓展参数
	private Context ctx;
	private String _payType;

	private String pay_id;
	private String order_id;
	private String pay_token;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(MResource.getIdByName(this, Constants.Resouce.LAYOUT,
				"ttw_alipay_pay"));
		this.ctx = getApplicationContext();

		Intent intent = getIntent();
		roleid = intent.getStringExtra("roleid");
		serverid = intent.getStringExtra("serverid");
		charge_money = intent.getDoubleExtra("money", 0);
		productname = intent.getStringExtra("productname");
		productdesc = intent.getStringExtra("productdesc");
		fcallbackurl = intent.getStringExtra("fcallbackurl");
		attach = intent.getStringExtra("attach");

		pay_id = intent.getStringExtra("pay_id");
		order_id = intent.getStringExtra("order_id");
		pay_token = intent.getStringExtra("pay_token");
		_payType =  intent.getStringExtra("pay_type");// 汇付宝规定，"30"就是启动微信模块
		pay();
	}

	public void pay() {

		DialogUtil.showDialog(HeePayActivity.this, "正在努力的加载...");
		NetUtil.reqHeepay(YTAppService.userinfo.mem_id,
				YTAppService.userinfo.user_token, charge_money + "", pay_token,
				ctx,_payType, new NetCallBack() {

					@Override
					public void onInitSuccess(ResultCode result) {
						String data = result.data;
						if (!TextUtils.isEmpty(data)) {
							JSONObject o;
							try {
								o = new JSONObject(data);
								String TokenID = o.getString("c");
								String orderid = o.getString("a");
								String heepayagentid = o.getString("b");
								if (TextUtils.isEmpty(TokenID)) {
									Util.onPayReqFail("tokenId is null",
											charge_money, HeePayActivity.this);
									return;
								}
								if (TextUtils.isEmpty(orderid)) {
									Util.onPayReqFail("orderid is null",
											charge_money, HeePayActivity.this);
									return;
								}
								if (TextUtils.isEmpty(heepayagentid)) {
									Util.onPayReqFail("heepeyagentid is null",
											charge_money, HeePayActivity.this);
									return;
								}
								// 调用支付宝相关api，显示支付宝充值界面
								String paramStr = TokenID + "," + heepayagentid
										+ "," + orderid + "," + _payType;
								if (!Util.isWeixinAvilible(HeePayActivity.this)) {
									// Toast.makeText(atx, "没有安装好微信，亲",
									// 0).show();
									Util.onPayReqFail("没有安装好微信，亲",
											charge_money, HeePayActivity.this);
									return;
								}
								HeepayPlugin.pay(HeePayActivity.this, paramStr);
							} catch (JSONException e) {
								Toast.makeText(ctx, "result.data 转化异常",
										Toast.LENGTH_LONG).show();
								e.printStackTrace();
								return;
							}
						} else {
							Util.onPayReqFail("请求微信失败", charge_money,
									HeePayActivity.this);
						}
					}

					@Override
					public void onInitFail(ResultCode result) {
						Util.onPayReqFail("请求微信失败", charge_money,
								HeePayActivity.this);
					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == com.heepay.plugin.activity.Constant.RESULTCODE) {
			System.out.println("微信支付结果码:"
					+ data.getExtras().getString("respCode"));
			String str = data.getExtras().getString("respCode");
			// 失败/未知原因
			// String errorCode = data.getExtras().getString("error_code");
			// String resultMessage =
			// data.getExtras().getString("result_message");
			// System.out.println("微信支付结果信息:" + resultMessage);
			// System.out
			// .println("token_id:" + data.getExtras().getString("token_id"));
			// // 实际金额
			// System.out.println("pay_amt:" +
			// data.getExtras().getString("pay_amt"));
			// // 总金额
			// System.out.println("total_amt:"
			// + data.getExtras().getString("total_amt"));
			// 支付类型
			// System.out.println("pay_type:" +
			// data.getExtras().getInt("pay_type"));
			if (str != null) {
				if (str.equals("01")) {
					Util.onPayReqSuc("支付成功", charge_money, HeePayActivity.this);
				} else if (str.equals("02")) {
					Util.onPayReqFail("支付处理中", charge_money,
							HeePayActivity.this);
				} else if (str.equals("-1")) {
					Util.onPayReqFail("错误信息：支付失败", charge_money,
							HeePayActivity.this);
				} else {
					Util.onPayReqFail("错误信息：支付失败", charge_money,
							HeePayActivity.this);
				}

			}
		}else{
			Util.onPayReqFail("错误信息：支付失败", charge_money,
					HeePayActivity.this);
		}
	}
}
